﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Leo.Blog.Model;
using System.Linq.Expressions;

namespace Leo.Blog.DAL.SysModule
{
    public class Sys_Account_D:BaseRepository<Sys_Account>
    {
        public static List<Sys_Account> GetAccounts(Sys_Account account, int orgId, int page, int rows, out int totalCount)
        {
            using (BlogEntities db = new BlogEntities())
            {
                IEnumerable<Sys_Account> accountList = db.Sys_Account;
                if (!string.IsNullOrEmpty(account.Account))
                {
                    accountList = from al in accountList
                                  where al.Account.Contains(account.Account)
                                  select al;
                }
                if (!string.IsNullOrEmpty(account.Name))
                {
                    accountList = from al in accountList
                                  where al.Name.Contains(account.Name)
                                  select al;
                }
                if (!string.IsNullOrEmpty(account.TelePhone))
                {
                    accountList = from al in accountList
                                  where al.TelePhone.Contains(account.TelePhone)
                                  select al;
                }
                if (account.IsEnable != -1)
                {
                    accountList = from al in accountList
                                  where al.IsEnable == account.IsEnable
                                  select al;
                }
                if (orgId != -1)
                {
                    accountList = from al in accountList
                                  join org in db.Sys_OrgUser on al.Id equals org.AccountId
                                  where org.OrgId==orgId
                                  select al;
                }
                totalCount = accountList.Count();
                accountList = accountList.OrderByDescending(m=>m.CreateDT)
                    .Skip(rows * (page - 1)) //越过多少条
                    .Take(rows).AsQueryable(); //取出多少条

                return accountList.ToList();
            }
        }

        public static List<Sys_Account> GetExportAccounts(Sys_Account account, int orgId)
        {
            using (BlogEntities db = new BlogEntities())
            {
                IEnumerable<Sys_Account> accountList = db.Sys_Account;
                if (!string.IsNullOrEmpty(account.Account))
                {
                    accountList = from al in accountList
                                  where al.Account.Contains(account.Account)
                                  select al;
                }
                if (!string.IsNullOrEmpty(account.Name))
                {
                    accountList = from al in accountList
                                  where al.Name.Contains(account.Name)
                                  select al;
                }
                if (!string.IsNullOrEmpty(account.TelePhone))
                {
                    accountList = from al in accountList
                                  where al.TelePhone.Contains(account.TelePhone)
                                  select al;
                }
                if (account.IsEnable != -1)
                {
                    accountList = from al in accountList
                                  where al.IsEnable == account.IsEnable
                                  select al;
                }
                if (orgId != -1)
                {
                    accountList = from al in accountList
                                  join org in db.Sys_OrgUser on al.Id equals org.AccountId
                                  where org.OrgId == orgId
                                  select al;
                }

                return accountList.ToList();
            }
        }
    }
}
